#include <iostream>
#include <QtGui>
#include <stdexcept>
#include "createitem.h"
#include "createmapitem.h"
#include "createmap.h"
#include "welcome.h"
#include "mainwindow.h"
#include "../location.h"

using namespace std;

MainWindow::MainWindow()
{
    welcome = new Welcome;
    setCentralWidget(welcome);
    createActions();
    createMenus();
}

// Create all the actions in the create menu and connect 
// them to the appropriate slot
void MainWindow::createActions()
{
	// CreateItem menu action
	createItemAction = new QAction(tr("&Item Blueprint"), this);
	createItemAction->setStatusTip(tr("Create a new item blueprint"));
	connect(createItemAction, SIGNAL(triggered()), this, SLOT(editItem()));

	// CreateMapItem menu action
	createMapItemAction = new QAction(tr("&MapItem Blueprint"), this);
	createMapItemAction->setStatusTip(tr("Create a new mapitem blueprint"));
	connect(createMapItemAction, SIGNAL(triggered()), this, SLOT(editMapItem()));

	// CreateNewMap menu action
	createMapAction = new QAction(tr("Map"), this);
	createMapAction->setStatusTip(tr("Create a new map"));
	connect(createMapAction, SIGNAL(triggered()),this, SLOT(editMap()));
}

// Add the actions to the create menu
void MainWindow::createMenus()
{
	createMenu = menuBar()->addMenu(tr("&Create"));
	createMenu->addAction(createItemAction);
	createMenu->addAction(createMapItemAction);
	createMenu->addAction(createMapAction);
}

// Slot for create item blueprint
void MainWindow::editItem()
{
	void deleteDialogs();
	createItem = new CreateItem;
	setCentralWidget(createItem);	
}

// Slot for create mapitem blueprint
void MainWindow::editMapItem()
{
	void deleteDialogs();
	createMapItem = new CreateMapItem;
	setCentralWidget(createMapItem);
}

// Slot for creating a new location (map)
void MainWindow::editMap()
{
	void deleteDialogs();
	createMap = new CreateMap;
	setCentralWidget(createMap);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	event->accept();
}

void MainWindow::deleteDialogs()
{
	if(welcome != NULL)
	{
		delete welcome;	
	}
	if(createItem != NULL)
	{
		delete createItem;	
	}
	if(createMapItem != NULL)
	{
		delete createMapItem;	
	}
	if(createMap != NULL)
	{
		delete createMap;	
	}
}
